package echartBusiness

import grails.converters.JSON
import grails.converters.XML
import net.sf.json.JSONObject
import net.sf.json.util.JSONBuilder
import net.sf.json.xml.XMLSerializer
import org.apache.commons.lang.StringEscapeUtils
import sysUtil.BeetlUtil
import sysUtil.JsoupUtil
import sysUtil.SysCommUtil

class EchartTestController {

    def index() {
        def tempOption = params.tempOption
        def formNode = params.formNode
        params.remove("tempOption")
        params.remove("formNode")

        def option = BeetlUtil.getStrTemplate(params,tempOption)
        option =  SysCommUtil.parseObj2Json(option)
        def formValue = SysCommUtil.parseObj2Json(params)
        println formValue.toString()
        def file = SysCommUtil.getEchartTempleteXMLFile("echart-line-01")
        def xml = """
        <echart>
            <tempOption>
                ${option.toString()}
            </tempOption>
            <formNode>
                ${formNode}
            </formNode>
            <formValue>
                ${formValue.toString()}
            </formValue>
        </echart>
        """

        def  jsoup = JsoupUtil.getJsoupInstanceFromDom(xml)
        println  jsoup.select(".layui-form").get(0).attr("lay-filte","echart-panelId")
        file.write(jsoup.toString())
        System.gc()
        //file.append(option.toString())
        render option.toString()
    }

    def index2() {
        def file = SysCommUtil.getEchartTempleteXMLFile("67")
        def xml =  file.getText()
        def  jsoup = JsoupUtil.getJsoupInstanceFromDom(xml)

        def option = jsoup.selectFirst("tempoption").html()
        option =  BeetlUtil.getStrTemplate(params,option)

        def formValue =  SysCommUtil.parseObj2Json( option )
        println formValue.toString(2)
        def result = new JSONObject()
        result.put("option",option)
        result.put("formValue",formValue.toString(4))
        render result.toString()
    }

    def test(){
        def json =  """
    {
    "EQPID": "DB-447",
    "USERNAME": "10014284",
    "CHECKTYPE": "1",
    "FLOG": "false",
    "INPUTDATALIST": {
        "INPUTDATAINFO": [
            {
                "DATATYPE": "Radio",
                "DATAVALUE": "OK,",
                "INPUTCNNAME": "框架材料",
                "INPUTNAME": "框架材料",
                "ISNUMBER": false
            },
            {
                "DATATYPE": "Radio",
                "DATAVALUE": "NG,",
                "INPUTCNNAME": "粘片胶材料",
                "INPUTNAME": "粘片胶材料",
                "ISNUMBER": false
            },
            {
                "DATATYPE": "Radio",
                "DATAVALUE": "NG,",
                "INPUTCNNAME": "粘片方向（角度）",
                "INPUTNAME": "粘片方向（角度）",
                "ISNUMBER": false
            },
            {
                "DATATYPE": "Radio",
                "DATAVALUE": "NG,rr",
                "INPUTCNNAME": "胶水溢出",
                "INPUTNAME": "胶水溢出",
                "ISNUMBER": false
            },
            {
                "DATATYPE": "Text",
                "DATAVALUE": "123",
                "INPUTCNNAME": "胶水攀爬高度",
                "INPUTNAME": "胶水攀爬高度",
                "ISNUMBER": true
            },
            {
                "DATADETAIL": "测试数据项1；测试数据项2；测试数据项3；",
                "DATATYPE": "Combo",
                "DATAVALUE": "测试数据项1",
                "INPUTCNNAME": "芯片1粘片位置测量",
                "INPUTNAME": "芯片1粘片位置测量",
                "ISNUMBER": false
            },
            {
                "DATADETAIL": "测试数据项1；测试数据项2；测试数据项3；",
                "DATATYPE": "Combo",
                "DATAVALUE": "测试数据项3",
                "INPUTCNNAME": "芯片1粘片位置测量",
                "INPUTNAME": "芯片1粘片位置测量",
                "ISNUMBER": false
            },
            {
                "DATATYPE": "Radio",
                "DATAVALUE": "NG,zz",
                "INPUTCNNAME": "最终检测结果",
                "INPUTNAME": "最终检测结果",
                "ISNUMBER": false
            }
        ]
    }
}
    """

        def    option =  SysCommUtil.parseObj2Json(json)

       //def xml = getXmlByJSONHtml(option)
        XMLSerializer xmlSerializer = new XMLSerializer();
        def xml =  xmlSerializer.write(option)
        println xml
        render xml
    }

     def   getXmlByJSONHtml(Object obj) {
        def xml
        if (obj) {
            org.json.JSONObject jsonObject = new org.json.JSONObject(obj)
            xml = org.json.XML.toString(jsonObject)
            xml = StringEscapeUtils.unescapeHtml(xml)
        }

        return xml
    }
}
